import { reqOrderAddress, reqOrderInfo } from '../../../../../pages/api/orderpay'
const app = getApp()
Page({
  data: {
    address: [],
    buyName: '', // 订购人姓名
    buyPhone: '', // 订购人手机号
    deliveryDate: '选择送达日期', // 期望送达日期
    blessing: '', // 祝福语
    show: false, // 期望送达日期弹框
    minDate: new Date().getTime(),
    currentDate: new Date().getTime(),
    cartInfo: [],
    total: ''
  },

  // 选择期望送达日期
  onShowDateTimerPopUp() {
    this.setData({
      show: true
    })
  },

  // 期望送达日期确定按钮
  onConfirmTimerPicker(event) {
    this.setData({
      show: false
    })
  },

  // 期望送达日期取消按钮 以及 关闭弹框时触发
  onCancelTimePicker() {
    this.setData({
      show: false,
      minDate: new Date().getTime(),
      currentDate: new Date().getTime()
    })
  },

  // 跳转到收货地址
  toAddress() {
    wx.navigateTo({
      url: '/modules/settingModule/pages/address/list/index'
    })
  },
  //获取地址
  async orderAddress() {
    const id = app.globalData.address.id
    if (id) {
      this.setData({
        address: app.globalData.address
      })
      return
    }
    const res = await reqOrderAddress()
    this.setData({
      address: res.data
    })
  },
  //获取商品详情
  async getOrderInfo() {

    const res = await reqOrderInfo()
    const orderGoods = res.data.cartVoList.find((item) => item.blessing !== '')
    this.setData({
      cartInfo: res.data.cartVoList,
      blessing: orderGoods,
      total: res.data.totalAmount
    })
  },
  onShow() {
    this.orderAddress()
    this.getOrderInfo()
  }
})
